package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.LogDAO;

/**
 * Servlet implementation class insertLog
 */
public class insertLog extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public insertLog() {
        super();
        // TODO Auto-generated constructor stub
    }

    private static String getTime()
    {
    	Date today=Calendar.getInstance(Locale.US).getTime();//返回当前中国地区的日期时间
    	String strToday=DateFormat.getDateTimeInstance().format(today);//格式化日期对象为字符串
    	/*SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss aa",Locale.US);
		Date strtoday = null;
		try {
			strtoday = dateFormat.parse(strToday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		return strToday;
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		
		String flag = request.getParameter("data");
		String obj = request.getParameter("obj");
		
        

		HttpSession session = request.getSession();
		String userName = session.getAttribute("trueName").toString();
		
		LogDAO ld = new LogDAO();
		
		if(flag.equals("1"))//已借出
		{
			ld.addUserLog(obj,getTime());
			out.print("{success:true,msg:'out,update the log'}");
		}
		else//已归还
		{
			ld.updateUserLog(obj, getTime());
			out.print("{success:false,msg:'in,update the log'}");
		}		
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
